package com.xxyuan.batch._13_flow_step;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableBatchProcessing
public class FlowStepJob {
    /**
     * job调度器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /***
     * job构造器工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step构造器工厂
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造一个step对象执行的任务(逻辑对象)
     */
    @Bean
    public Tasklet taskletA() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletA----------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletB1() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("----------stepB----taskletB1----------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletB2() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("----------stepB----taskletB2----------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletB3() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("----------stepB----taskletB3----------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet taskletC() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("--------------taskletC----------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step stepA() {
        return stepBuilderFactory.get("stepA")
                .tasklet(taskletA())
                .build();
    }

    @Bean
    public Step stepC() {
        return stepBuilderFactory.get("stepC")
                .tasklet(taskletC())
                .build();
    }

    @Bean
    public Step stepB1() {
        return stepBuilderFactory.get("stepB1")
                .tasklet(taskletB1())
                .build();
    }

    @Bean
    public Step stepB2() {
        return stepBuilderFactory.get("stepB2")
                .tasklet(taskletB2())
                .build();
    }

    @Bean
    public Step stepB3() {
        return stepBuilderFactory.get("stepB3")
                .tasklet(taskletB3())
                .build();
    }

    @Bean
    public Step stepB() {
        return stepBuilderFactory.get("stepB")
                .flow(flowB())
                .build();
    }

    /**
     * 构建一个流式步骤
     */
    @Bean
    public Flow flowB() {
        return new FlowBuilder<Flow>("flowB")
                .start(stepB1())
                .next(stepB2())
                .next(stepB3())
                .build();
    }

    /**
     *  如果firstStep执行成功，下一步执行successStep，否则执行failStep
     *  *为任意返回值
     */
    @Bean
    public Job job() {
        return jobBuilderFactory.get("flow-Step-job")
                .start(stepA())
                .next(stepB())
                .next(stepC())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(FlowStepJob.class, args);
    }
}